package com.yihu.cms.action;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.yihu.cms.util.MyCustomDateEditor;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public abstract class BaseController {
	
	protected HttpServletRequest request;  
    protected HttpServletResponse response;  
    protected HttpSession session;  
    protected final Logger log = LoggerFactory.getLogger(BaseController.class);
    @InitBinder    
	public void initBinder(WebDataBinder binder) {    
	    binder.registerCustomEditor(Date.class, new MyCustomDateEditor());    
	}  
    @ModelAttribute  
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response){  
        this.request = request;  
        this.response = response;  
        this.session = request.getSession();  
    } 
	
	
	public Map<String, Object> getResult(int resultCode, Object info) {
		String message = "";
		if (resultCode == 0)
			message = "success";
		else
			message = "fail";

		Map<String, Object> mapResult = new HashMap<String, Object>();
		mapResult.put("result", resultCode);
		mapResult.put("message", message);
		mapResult.put("info", info);
		return mapResult;
	}
	
	public Map<String, Object> getResult(int resultCode) {
		String message = "";
		if (resultCode == 0)
			message = "success";
		else
			message = "fail";

		Map<String, Object> mapResult = new HashMap<String, Object>();
		mapResult.put("result", resultCode);
		mapResult.put("message", message);
		return mapResult;
	}
	
	public static String encodeStr(String str) { 
		if(str == null)
			return "";
        try {  
            return new String(str.getBytes("ISO-8859-1"), "UTF-8");  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
            return "";  
        }  
    }

}
